﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Xml;
using System.Windows.Forms;

using OneNoteAddin;
using OneNote = Microsoft.Office.Interop.OneNote;
using Microsoft.Office.OneNote;
using System.IO;
using System.Drawing;
using Microsoft.Win32;

namespace EMRNotes
{
    [Guid("681B64A0-A58F-4eec-8257-19048F289833")]
    public class EMRRecord : IOneNoteAddIn
    {
        private OneNote.Application appl = null;

        public EMRRecord()
        {
           appl = new Microsoft.Office.Interop.OneNote.Application();
        }
        #region IOneNoteAddIn Members


        public bool OnClick(string strActivePageID)
        {
            try
            {
                EMRAddRecordForm frm = new EMRAddRecordForm();
                DialogResult dr = frm.ShowDialog();

                if (dr == DialogResult.OK)
                {
                    //OneNoteNotebook newPatientNoteboook = null;

                    OneNoteSection sec = null;

                    //NotebookRoot root = Microsoft.Office.OneNote.Application.NotebookRoot;

                    string templatesFolder = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\Software\\MCD-soft\\OneNoteEMR", "TemplatesFolderPath", "C:\\Templates\\");
                    string userName = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\Software\\MCD-soft\\OneNoteEMR", "UserName", "-");
                    string userTitle = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\Software\\MCD-soft\\OneNoteEMR", "UserTitle", "-");

                    string notebooksXML;
                    OneNoteNotebook currentNoteboook = null; // (OneNoteNotebook)OneNoteHierarchy.Current.CurrentlyViewedPage.Parent.Parent;

                    appl.GetHierarchy("", Microsoft.Office.Interop.OneNote.HierarchyScope.hsNotebooks, out notebooksXML);

                    XmlDocument xml = new XmlDocument();
                    xml.LoadXml(notebooksXML);

                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
                    nsmgr.AddNamespace("one", "http://schemas.microsoft.com/office/onenote/2007/onenote");


                    string currentNotebookID = xml.SelectSingleNode("//one:Notebook[@isCurrentlyViewed='true']", nsmgr).Attributes["ID"].Value;

                    foreach (OneNoteNotebook notebook in OneNoteHierarchy.Current.Notebooks)
                    {
                        if (notebook.ObjectId.ToString() == currentNotebookID)
                        {
                            currentNoteboook = notebook;
                            break;
                        }

                    }

                    string sectionName;
                        
                    switch (frm.RecordType)
                        {
                            case 1:
                                sectionName = Resource1.Encounters;
                            break;

                            case 2:
                                sectionName = Resource1.Orders;
                            break;

                            case 3:
                                sectionName = Resource1.Examinations;
                            break;

                            default:
                                sectionName = Resource1.Orders;
                            break;

                        }

                        sec = currentNoteboook.OpenSection(sectionName);

                        OneNotePage pg = sec.CreatePage(); 

                        // Получить содержимое шаблона страницы из файла
                        String strImportXML;
                        if (frm.TemplateFile != Resource1.NotUsed)
                        {
                            strImportXML = Resource1.RecordImageTemplate;
                            strImportXML = strImportXML.Replace("[IMAGE_PATH]", Path.Combine(templatesFolder, frm.TemplateFile));  
                        }
                        else
                            strImportXML = Resource1.RecordEmptyTemplate;
                    
                        strImportXML = strImportXML.Replace("[PAGE_ID]", pg.ObjectId.ToString());
                        strImportXML = strImportXML.Replace("[PAGE_TITLE]", userTitle + " - " + userName);

                        appl.UpdatePageContent(strImportXML, DateTime.MinValue);

                        appl.NavigateTo(pg.ObjectId.ToString(), null, false);

                    }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, Resource1.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        public bool OnEvent(Microsoft.Office.Interop.OneNote.OneNoteAddIn_Event evt, string strParameter)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
